// index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Word: '',//关键词
    CurrentPage: 1,
    Categorys: [],
    selCatId: 0,
    selCatName: '',
    Brands: [],
    selBrandId: 0,
    selBrandName: '',
    Prices: [],
    selPriceId: 0,
    selPriceName: '',
    isCategoryTap: false,
    isBrandTap: false,
    isPriceTap: false,
    showSearch: false,
    searchCategory: [],//当前搜索类型
    goodsResult: [],
    loadingMoreHidden: false,
    selectedMenuId: 0,
    categoriesTwo: [],
    classLogo: "",
    classProduct: [],
    showProduct: false,
    imgsrc: "",
    logoclassOne: "",
    selectid: 0,
    selectname: ""
  },
  tapSearch: function (e) {
    wx.navigateTo({
      url: "/pages/classes/index"
    })
  },
  //查看更多
  toClasses: function () {
    wx.navigateTo({
      url: '/pages/classes/index?id=' + this.data.selectid + '&name=' + this.data.selectname,
    })
  },
  selectMenu: function (e) {
    var that = this
    var index = e.currentTarget.dataset.indexid
    var id = e.currentTarget.dataset.id
    var name = e.currentTarget.dataset.name
    var imgsrc = e.currentTarget.dataset.imgsrc
    that.setData({
      selectedMenuId: index,
      imgsrc: imgsrc,
      selectid: id,
      selectname: name
    })
    that.searchGoods(id, that.data.categoriesTwo)
  },
  toDetailsTap: function (e) {
    var that = this
    console.log(app.globalData.storeId)
    var pid = e.currentTarget.dataset.id
    //获取拼团信息
    console.log(app.globalData.storeId)
    wx: wx.request({
      url: app.globalData.host + '/api/Store/GetGroupBuyProductInfo?storeId=' + app.globalData.storeId + '&pid=' + pid,
      success: function (res) {
        console.log(res)
        if (res.data.Code == 0) {
          wx.navigateTo({
            url: '/pages/pintuan-details/index?id=' + pid,
          })
        } else {
          wx.navigateTo({
            url: "/pages/goods-details/index?id=" + pid
          })
        }
      }
    })
  },

  addShopCar: function (e) {
    var pid = e.currentTarget.dataset.id
    var that = this;
    if (this.data.buyNumber < 1) {
      wx.showModal({
        title: '提示',
        content: '暂时缺货哦~',
        showCancel: false
      })
      return;
    }

    wx.request({
      url: app.globalData.host + '/api/cart/AddProduct',
      data: {
        uid: app.globalData.userInfo.Uid,
        pid: pid,
        buyCount: 1
      },
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            shopNum: res.data.Data
          });
          wx.showToast({
            title: '加入购物车成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    wx.request({
      url: app.globalData.host + '/api/store/GetStoreClassList',
      data: {
        storeId: app.globalData.storeId
      },
      success: function (res) {
        var categories = [];
        var categoriesTwo = [];
        if (res.data.Code == 0) {
          var logoclassOne = app.globalData.image_logo_class + res.data.Data[0].Logo
          for (var i = 0; i < res.data.Data.length; i++) {
            var temp = res.data.Data[i];
            if (temp.ParentId == 0)//只展示一级类目
            {
              var c = {};
              c.parentid = temp.ParentId
              c.id = temp.StoreCid;
              c.name = temp.Name;
              c.Logo = app.globalData.image_logo_class + temp.Logo;
              categories.push(c);
            }
            if (temp.HasChild == 1 && temp.Layer == 1 || temp.Layer > 2) {
              continue;
            } else {
              if (temp.Layer == 2 || temp.HasChild == 0) {
                var h = {};
                h.id = temp.StoreCid;
                h.name = temp.Name;
                h.Layer = temp.Layer;//等级
                h.ParentId = temp.ParentId;//父级id，0是最大级
                h.Logo = app.globalData.image_logo_class + temp.Logo;//分类图片
                h.HasChild = temp.HasChild//是否有子类
                h.productArr = [];
                categoriesTwo.push(h);
              }
            }
          }
          that.setData({
            Categorys: categories,
            categoriesTwo: categoriesTwo,
            logoclassOne: logoclassOne,
            selectid: categories[0].id,
            selectname: categories[0].name,
          });
          that.searchGoods(categories[0].id, categoriesTwo)
        }
      }
    })
  },
  searchGoods: function (cid, categories) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var classProduct = []
    for (var j = 0; j < categories.length; j++) {
      if (cid == categories[j].ParentId && categories[j].Layer == 2 || categories[j].HasChild == 0 && cid == categories[j].id) {
        var f = categories[j];
        categories[j].productArr = []
        classProduct.push(f)
      }
    }
    wx.request({
      url: app.globalData.host + '/api/store/Search',
      data: {
        storeId: app.globalData.storeId,
        cateId: cid,
        word: "",
        page: 1,
        pageSize: 10,
        priceStart: 0,
        priceEnd: 0
      },
      success: function (res) {
        var newbs = [];
        var rawData = res.data.Data;
        var products = rawData.ProductList;
        var showProduct = false
        for (var k = 0; k < classProduct.length; k++) {
          for (var i = 0; i < products.length; i++) {
            if (classProduct[k].id == products[i].StoreCid) {
              var d = products[i];
              if (d.SKUGid == 0) {
                d.ShowImg = app.globalData.image800_800host + d.ShowImg;
                classProduct[k].productArr.push(d);
                showProduct = true
              }
              else {
                if (!that.checkProduct(classProduct[k].productArr, d.SKUGid)) {
                  d.ShowImg = app.globalData.image800_800host + d.ShowImg;
                  classProduct[k].productArr.push(d);
                  showProduct = true
                }
              }
            }
          }
        }
        that.setData({
          classProduct: classProduct,
          classLogo: classProduct[0].Logo,
          goodsResult: newbs,
          showProduct: showProduct
        });
        wx.hideLoading()
      }
    })
  },
  //sku产品去重
  checkProduct: function (products, text) {
    var state = false;  //默认不存在
    if (products.length > 0 & text != 0) {
      if (products.length > 0) {
        for (var i = 0; i < products.length; i++) {
          if (text == products[i].SKUGid) {
            state = true; //已经存在
            break;
          }
        }
      }
    }
    return state;
  },
  onPullDownRefresh: function () {
    var that = this;
    wx.showToast({
      title: '正在刷新',
      duration: 1500,
      icon: 'loading',
      success: function () {
        that.setData({ CurrentPage: 1 });
        that.searchGoods();
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    })
  },
})